package cn.icanci.loopstack.bic.dal.mongo.mongo;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import cn.icanci.loopstack.bic.dal.mongo.common.PageList;
import cn.icanci.loopstack.bic.dal.mongo.daointerface.EdgeStyleDAO;
import cn.icanci.loopstack.bic.dal.mongo.dateobject.EdgeStyleDO;

/**
 * @author icanci
 * @since 1.0 Created in 2023/09/13 23:32
 */
@Service("edgeStyleDAO")
public class MongoEdgeStyleDAO extends AbstractBaseDAO<EdgeStyleDO> implements EdgeStyleDAO {
    @Override
    public void insert(EdgeStyleDO EdgeStyleDO) {
        super.insert(EdgeStyleDO);
        mongoTemplate.insert(EdgeStyleDO, COLLECTION_NAME);
    }

    @Override
    public void update(EdgeStyleDO EdgeStyleDO) {
        super.update(EdgeStyleDO);
        mongoTemplate.save(EdgeStyleDO, COLLECTION_NAME);
    }

    @Override
    public List<EdgeStyleDO> queryAll() {
        Criteria criteria = Criteria.where(EdgeStyleColumn.ENV).is(DEFAULT_ENV);
        Query query = new Query(criteria);
        return mongoTemplate.find(query, COLLECTION_CLASS, COLLECTION_NAME);

    }

    @Override
    public PageList<EdgeStyleDO> pageQuery(EdgeStyleDO EdgeStyleDO, int pageNum, int pageSize) {
        Criteria criteria = Criteria.where(EdgeStyleColumn.ENV).is(DEFAULT_ENV);
        if (StringUtils.isNotBlank(EdgeStyleDO.getEdgeStyleName())) {
            // 不分区大小写查询，其中操作符"i"：表示不分区大小写
            criteria.and(EdgeStyleColumn.EDGE_STYLE_NAME).regex("^.*" + EdgeStyleDO.getEdgeStyleName() + ".*$", "i");
        }
        if (StringUtils.isNotBlank(EdgeStyleDO.getEdgeStyleKey())) {
            // 不分区大小写查询，其中操作符"i"：表示不分区大小写
            criteria.and(EdgeStyleColumn.EDGE_STYLE_KEY).regex("^.*" + EdgeStyleDO.getEdgeStyleKey() + ".*$", "i");
        }
        if (StringUtils.isNotBlank(EdgeStyleDO.getSystemUuid())) {
            criteria.and(EdgeStyleColumn.SYSTEM_UUID).is(EdgeStyleDO.getSystemUuid());
        }
        Query query = new Query(criteria);
        query.with(Sort.by(Sort.Direction.DESC, EdgeStyleColumn.CREATE_TIME));
        return pageQuery(query, COLLECTION_CLASS, pageSize, pageNum, COLLECTION_NAME);
    }

    @Override
    public EdgeStyleDO queryOneById(String _id) {
        Criteria criteria = Criteria.where(EdgeStyleColumn._ID).is(_id);
        criteria.and(EdgeStyleColumn.ENV).is(DEFAULT_ENV);
        Query query = new Query(criteria);
        return mongoTemplate.findOne(query, COLLECTION_CLASS, COLLECTION_NAME);

    }

    @Override
    public Class<? extends BaseColumn> getBaseColumn() {
        return EdgeStyleColumn.class;
    }

    @Override
    public EdgeStyleDO queryBySystemUuidAndName(String systemUuid, String name) {
        Criteria criteria = Criteria.where(EdgeStyleColumn.EDGE_STYLE_NAME).is(name);
        criteria.and(EdgeStyleColumn.ENV).is(DEFAULT_ENV);
        criteria.and(EdgeStyleColumn.SYSTEM_UUID).is(systemUuid);
        Query query = new Query(criteria);
        return mongoTemplate.findOne(query, COLLECTION_CLASS, COLLECTION_NAME);

    }

    @Override
    public EdgeStyleDO queryBySystemUuidAndKey(String systemUuid, String key) {
        Criteria criteria = Criteria.where(EdgeStyleColumn.EDGE_STYLE_KEY).is(key);
        criteria.and(EdgeStyleColumn.ENV).is(DEFAULT_ENV);
        criteria.and(EdgeStyleColumn.SYSTEM_UUID).is(systemUuid);
        Query query = new Query(criteria);
        return mongoTemplate.findOne(query, COLLECTION_CLASS, COLLECTION_NAME);

    }

    @Override
    public EdgeStyleDO queryByUuid(String uuid) {
        Criteria criteria = Criteria.where(EdgeStyleColumn.UUID).is(uuid);
        criteria.and(EdgeStyleColumn.ENV).is(DEFAULT_ENV);
        Query query = new Query(criteria);
        return mongoTemplate.findOne(query, COLLECTION_CLASS, COLLECTION_NAME);
    }
}
